package org.dromara.business.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.NotEmpty;
import jakarta.validation.constraints.NotNull;
import lombok.RequiredArgsConstructor;
import org.dromara.business.domain.bo.UserFlowingBo;
import org.dromara.business.domain.vo.UserFlowingVo;
import org.dromara.business.service.UserFlowingService;
import org.dromara.common.core.domain.R;
import org.dromara.common.core.validate.AddGroup;
import org.dromara.common.core.validate.EditGroup;
import org.dromara.common.excel.utils.ExcelUtil;
import org.dromara.common.idempotent.annotation.RepeatSubmit;
import org.dromara.common.log.annotation.Log;
import org.dromara.common.log.enums.BusinessType;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.common.web.core.BaseController;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 用户积分流水
 *
 * @author zlj
 * @date 2024-08-16
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/business/flowing")
public class UserFlowingController extends BaseController {

    private final UserFlowingService userFlowingService;

    /**
     * 查询用户积分流水列表
     */
//    @SaCheckPermission("business:flowing:list")
    @GetMapping("/list")
    public TableDataInfo<UserFlowingVo> list(UserFlowingBo bo, PageQuery pageQuery) {
        return userFlowingService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出用户积分流水列表
     */
    @SaCheckPermission("business:flowing:export")
    @Log(title = "用户积分流水", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(UserFlowingBo bo, HttpServletResponse response) {
        List<UserFlowingVo> list = userFlowingService.queryList(bo);
        ExcelUtil.exportExcel(list, "用户积分流水", UserFlowingVo.class, response);
    }

    /**
     * 获取用户积分流水详细信息
     *
     * @param userFlowingId 主键
     */
    @SaCheckPermission("business:flowing:query")
    @GetMapping("/{userFlowingId}")
    public R<UserFlowingVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long userFlowingId) {
        return R.ok(userFlowingService.queryById(userFlowingId));
    }

    /**
     * 新增用户积分流水
     */
    @SaCheckPermission("business:flowing:add")
    @Log(title = "用户积分流水", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody UserFlowingBo bo) {
        return toAjax(userFlowingService.insertByBo(bo));
    }

    /**
     * 修改用户积分流水
     */
    @SaCheckPermission("business:flowing:edit")
    @Log(title = "用户积分流水", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody UserFlowingBo bo) {
        return toAjax(userFlowingService.updateByBo(bo));
    }

    /**
     * 删除用户积分流水
     *
     * @param userFlowingIds 主键串
     */
    @SaCheckPermission("business:flowing:remove")
    @Log(title = "用户积分流水", businessType = BusinessType.DELETE)
    @DeleteMapping("/{userFlowingIds}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] userFlowingIds) {
        return toAjax(userFlowingService.deleteWithValidByIds(List.of(userFlowingIds), true));
    }
}
